// 注册病虫防治页面处理程序
pageManager.registerPage('disease', {
    // 保存页面状态
    currentCrop: 'wheat',
    chartInstance: null,

    // 病虫害数据配置
    diseaseConfigs: {
        wheat: {
            name: '小麦',
            commonDiseases: [
                {
                    name: '白粉病',
                    image: 'images/wheat-powdery-mildew.jpg',
                    symptoms: '叶片、叶鞘和茎秆表面产生白色粉状物',
                    treatment: '喷施三唑酮、腈菌唑等杀菌剂'
                },
                {
                    name: '赤霉病',
                    image: 'images/wheat-scab.jpg',
                    symptoms: '麦穗呈现粉红色或淡红色霉状物',
                    treatment: '开花期喷施咪鲜胺、丙环唑等药剂'
                },
                {
                    name: '锈病',
                    image: 'images/wheat-rust.jpg',
                    symptoms: '叶片出现橙黄色或褐色粉状物',
                    treatment: '及时喷施三唑酮等保护性杀菌剂'
                }
            ],
            riskLevels: {
                temperature: {
                    low: { min: 0, max: 15 },
                    medium: { min: 15, max: 22 },
                    high: { min: 22, max: 30 }
                },
                humidity: {
                    low: { min: 0, max: 40 },
                    medium: { min: 40, max: 70 },
                    high: { min: 70, max: 100 }
                }
            }
        },
        rice: {
            name: '水稻',
            commonDiseases: [
                {
                    name: '稻瘟病',
                    image: 'images/rice-blast.jpg',
                    symptoms: '叶片出现褐色菱形斑，边缘发黑',
                    treatment: '及时喷施稻瘟灵等药剂防治'
                },
                {
                    name: '纹枯病',
                    image: 'images/rice-sheath-blight.jpg',
                    symptoms: '叶鞘出现灰白色病斑，边缘褐色',
                    treatment: '喷施井冈霉素等药剂'
                }
            ]
        },
        corn: {
            name: '玉米',
            commonDiseases: [
                {
                    name: '大斑病',
                    image: 'images/corn-blight.jpg',
                    symptoms: '叶片出现褐色大型病斑',
                    treatment: '喷施甲基硫菌灵等药剂'
                },
                {
                    name: '茎腐病',
                    image: 'images/corn-stalk-rot.jpg',
                    symptoms: '茎基部变软腐烂，易倒伏',
                    treatment: '加强排水，喷施防腐剂'
                }
            ]
        }
    },

    init() {
        // 初始化作物选择
        this.initCropSelect();
        // 更新页面数据
        this.updatePageData();
    },

    // 初始化作物选择
    initCropSelect() {
        const cropSelect = document.getElementById('diseaseCropSelect');
        if (cropSelect) {
            cropSelect.value = this.currentCrop;
            cropSelect.addEventListener('change', (e) => {
                this.currentCrop = e.target.value;
                this.updatePageData();
            });
        }
    },

    // 更新页面所有数据
    updatePageData() {
        this.updateCropInfo();
        this.updateRiskStatus();
        this.updateDiseaseList();
        this.updateTreatmentHistory();
    },

    // 更新作物信息
    updateCropInfo() {
        const config = this.diseaseConfigs[this.currentCrop];
        document.getElementById('cropType').textContent = config.name;
        // ... 更新其他作物信息
    },

    // 更新风险状态
    updateRiskStatus() {
        // 模拟环境数据
        const currentTemp = 25;
        const currentHumidity = 65;

        // 计算风险等级
        const riskLevel = this.calculateRiskLevel(currentTemp, currentHumidity);
        
        // 更新风险卡片
        document.querySelectorAll('.overview-cards .card').forEach(card => {
            const type = card.dataset.type;
            switch(type) {
                case 'risk':
                    card.querySelector('p').textContent = riskLevel.overall;
                    break;
                case 'suitability':
                    card.querySelector('p').textContent = riskLevel.suitability;
                    break;
                case 'severity':
                    card.querySelector('p').textContent = riskLevel.severity;
                    break;
                case 'treatment':
                    card.querySelector('p').textContent = riskLevel.suggestion;
                    break;
            }
        });
    },

    // 计算风险等级
    calculateRiskLevel(temp, humidity) {
        const config = this.diseaseConfigs[this.currentCrop].riskLevels;
        let tempRisk = 'low';
        let humidityRisk = 'low';

        // 计算温度风险
        if (temp >= config.temperature.high.min) {
            tempRisk = 'high';
        } else if (temp >= config.temperature.medium.min) {
            tempRisk = 'medium';
        }

        // 计算湿度风险
        if (humidity >= config.humidity.high.min) {
            humidityRisk = 'high';
        } else if (humidity >= config.humidity.medium.min) {
            humidityRisk = 'medium';
        }

        // 综合评估
        const riskMap = {
            high: 3,
            medium: 2,
            low: 1
        };

        const overallRisk = (riskMap[tempRisk] + riskMap[humidityRisk]) / 2;

        return {
            overall: overallRisk >= 2.5 ? '高风险' : overallRisk >= 1.5 ? '中等风险' : '低风险',
            suitability: humidityRisk === 'high' ? '非常适宜' : humidityRisk === 'medium' ? '较适宜' : '不适宜',
            severity: tempRisk === 'high' ? '重度' : tempRisk === 'medium' ? '中度' : '轻度',
            suggestion: overallRisk >= 2.5 ? '立即防治' : overallRisk >= 1.5 ? '需要预防' : '继续监测'
        };
    },

    // 更新病虫害列表
    updateDiseaseList() {
        const config = this.diseaseConfigs[this.currentCrop];
        const container = document.querySelector('.disease-list');
        
        if (container && config) {
            container.innerHTML = config.commonDiseases.map(disease => `
                <div class="disease-card">
                    <img src="${disease.image}" alt="${disease.name}图片">
                    <div class="disease-info">
                        <h4>${disease.name}</h4>
                        <p>特征：${disease.symptoms}</p>
                        <p>防治：${disease.treatment}</p>
                    </div>
                </div>
            `).join('');
        }
    },

    // 更新防治记录
    updateTreatmentHistory() {
        // 模拟防治记录数据
        const history = [
            {
                date: '2024-03-15',
                type: '白粉病',
                treatment: '喷施三唑酮',
                effect: '良好'
            },
            // ... 更多记录
        ];

        const tbody = document.getElementById('treatmentHistory');
        tbody.innerHTML = history.map(record => `
            <tr>
                <td>${record.date}</td>
                <td>${record.type}</td>
                <td>${record.treatment}</td>
                <td>${record.effect}</td>
            </tr>
        `).join('');
    },

    cleanup() {
        // 清理资源
    }
}); 